package com.wanmait.wanmarecruit.webController;

import com.wanmait.wanmarecruit.entity.*;
import com.wanmait.wanmarecruit.service.PositionIntroductionTabService;
import com.wanmait.wanmarecruit.service.PositionService;
import com.wanmait.wanmarecruit.service.PositionWelfareService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("web")
public class WebCompanyPositionController {

    @Resource
    private PositionService positionService;
    @Resource
    private PositionWelfareService positionWelfareService;
    @Resource
    private PositionIntroductionTabService positionIntroductionTabService;

    @RequestMapping("/companies/deleteOnePosition")
    public void deleteOnePosition(Integer positionId){
        positionService.updatePosition(positionId);
    }

    @RequestMapping("/companies/batchDele")
    public void batchDele(Integer[] positionIds){
        positionService.batchDele(positionIds);
    }

    @PostMapping("/companies/saveOnePosition")
    public String saveOnePosition(Position position, Integer[] existTec, Integer[] existTecs, HttpSession session)
    {
        Integer companyId = ((Recruiter)session.getAttribute("hr")).getCompanyId();
        position.setCompanyId(companyId);
        position.setEnable(false);
        positionService.save(position);
        List<PositionWelfare> positionWelfares = new ArrayList<>();
        List<PositionIntroductionTab> positionIntroductionTabs = new ArrayList<>();

        for (int i = 0; i < existTec.length; i++) {
            PositionIntroductionTab positionIntroductionTab = new PositionIntroductionTab();
            positionIntroductionTab.setPositionId(position.getId());
            positionIntroductionTab.setIntroductionTabId(existTec[i]);
            positionIntroductionTabs.add(positionIntroductionTab);
        }
        positionIntroductionTabService.saveBatch(positionIntroductionTabs);
        for (int i = 0; i < existTecs.length; i++) {
            PositionWelfare positionWelfare = new PositionWelfare();
            positionWelfare.setPositionId(position.getId());
            positionWelfare.setWelfareId(existTecs[i]);
            positionWelfares.add(positionWelfare);
        }
        positionWelfareService.saveBatch(positionWelfares);
        return "redirect:/web/companies/publishNewPosition?success=1";
    }
}
